// pages/pay/payTransit/payTransit.js
const app = getApp()
const WXAPI = require("../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: ' ', // 导航栏 中间的标题
      textColor: '#333', // 标题颜色
      bgColor: '#FAFAFA', // 导航栏背景颜色
      backUrl: '/pages/userMarket/userMarket'
    },
    merchantId: '',//商家ID
    cardList:[],
    qrCodeUrl:'',
    isWx: false, //微信扫码进来
    anonymity: '', //1 匿名用户 2 非匿名用户
    staffId: '',
    wxUrl: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _options: options,
    })
    var token = wx.getStorageSync('token')
    console.log("哈哈哈啊哈哈啊哈", token)
    if (token == '') {
      this.login() 
      return false
    }
    //蜜链盟用户端扫码进入
    // var QRmerchantId = '1419584445677113345'
    var staffId = options.staffId
    var QRmerchantId = options.QRmerchantId
    if (QRmerchantId) {
      console.log("扫二维码支付")
      this.setData({
        merchantId: QRmerchantId,
        staffId: staffId
      })
      this.getCardType()
    }

     //微信客户端扫码支付
     var wxUrl = decodeURIComponent(options.q)
    var qq = wxUrl
     if (wxUrl && wxUrl != 'undefined') {
       var num = wxUrl.indexOf("?")
       wxUrl = wxUrl.substring(num + 1); //
       wxUrl = wxUrl.split("code=");
       wxUrl = wxUrl[1]
       wxUrl = JSON.parse(wxUrl)
       console.log("微信扫二维码支付", )
       this.setData({
         qrCodeUrl: wxUrl.param,
         isWx: true,
         staffId: wxUrl.staffId,
       })
       this.getParCollectionQrCodeUrl()
     }
     this.setNavSize()
  },
  

  //获取可有卡类型
  getCardType(){
    let that = this
    wx.showLoading({
      title: '加载中',
    })    //加载动画  
    var data = {
      merchantId: this.data.merchantId
    }
    WXAPI.canUseCard( data, (res) =>{
      const data = res.result
      if(res.code === 200){
        wx.hideLoading()
        console.log("获取可有卡类型",data)
     
        this.setData({
          cardList: data,
        })
        this.judgePath()
      }
    })
  },
  judgePath(){
    var cardList = this.data.cardList
    if(cardList.length == 1){
      if(cardList[0].cardType == 2 || cardList[0].cardType == 4){
        wx.redirectTo({
          url: '/pages/pay/payList/payList?merchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&anonymity='+this.data.anonymity+'&staffId='+this.data.staffId
        })
        // if(cardList[0].cardIdList.length == 1){
        //   wx.redirectTo({
        //     url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/otherCarddetail?assetId='+cardList[0].cardIdList[0].assetId+'&id='+cardList[0].cardIdList[0].id+'&cardTypeShow='+cardList[0].cardType+'&pathType=payTransit',   
        //   })
        // }else{
        //   wx.redirectTo({
        //     url: '/pages/pay/payList/payList?merchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&anonymity='+this.data.anonymity+'&staffId='+this.data.staffId
        //   })
        // }
        
      }else{
        wx.clearStorageSync()
        wx.redirectTo({
          url: '/pages/pay/pay?QRmerchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&staffId='+this.data.staffId 
        })
      } 
    }
    else if(cardList.length >= 2){
      var arr = []
      cardList.map(item =>{
        arr.push(item.cardType)
      })
      var gu = arr.indexOf('1')
      if(gu > 0){
        wx.clearStorageSync()
        wx.redirectTo({
          url: '/pages/pay/pay?QRmerchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&staffId='+this.data.staffId,   
        })
      }else{
        wx.redirectTo({
          url: '/pages/pay/payList/payList?merchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&anonymity='+this.data.anonymity+'&staffId='+this.data.staffId
        })
      }
     
    }else{
      wx.clearStorageSync()
      wx.redirectTo({
        url: '/pages/pay/pay?QRmerchantId='+this.data.merchantId+'&isWx='+this.data.isWx+'&staffId='+this.data.staffId,   
      })
    }
  },
  //解析二维码confirmInfo
  getParCollectionQrCodeUrl() {
    const that = this
    var data = {
      qrCodeUrl: this.data.qrCodeUrl,
      // qrCodeUrl:'https://bee.xiaodaiyan.com/pay/uQRxY%2F6Y4pbfVN4%2BbISi5WEG2fcys%2Beg9h2%2B0dfXuU8%3D',  
    }
    WXAPI.parCollectionQrCodeUrl(data, (res) => {
      const data = res.result
      console.log("解析二维码", res)
      if (res.code === 200) {
        this.setData({
          merchantId: data.merchantId,
        })
        this.getCardType()
      }
    })
  },
  login() {
    // 登录 
    let that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that.setData({
            code: res.code
          })
          console.log('res.code',res.code)
          that.getToken()
        } else {
          console.log('获取code失败', res.errMsg)
        }
      }
    })
  },
  //匿名支付获取token
  getToken() {
    var code = this.data.code
    console.log('code', code)
    var data = {
      code: code,
      appid: app.globalData.appid,
    }
    WXAPI.silentAuthorization(data, (res) => {
      const data = res.result
      console.log("获取token", res)
      if (res.code === 200) {
        wx.setStorageSync('token', data.token)
        wx.setStorageSync('phone', data.phone)
        this.onLoad(this.data._options)
        this.setData({
          anonymity: data.anonymity,
        })
      }
    })
  },
   //关闭页面
   chosePage(){
    if(this.data.anonymity == 1){
      wx.clearStorageSync()
      console.log('清空token',)
    }
    wx.reLaunch({
      url: '/pages/userMarket/userMarket',
    })
  },
  // 通过获取系统信息计算导航栏高度
  setNavSize: function() {
    var that = this
        , sysinfo = wx.getSystemInfoSync()
        , statusHeight = sysinfo.statusBarHeight
        , isiOS = sysinfo.system.indexOf('iOS') > -1
        , navHeight;
    if (!isiOS) {
        navHeight = 48;
    } else {
        navHeight = 44;
    }
    console.log('statusHeight',statusHeight)
    console.log('navHeight',navHeight)
    that.data.top = statusHeight + navHeight
    that.setData({
        status: statusHeight,
        navHeight: navHeight
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})